import { Entity, Columns, Id, ColumnType, Convert } from '@ohos/dataorm';
import List from '@ohos.util.List';
import { Folder } from './Folder';
import { Type } from './Type';
import { LabelConverter, ListItemsConverter, SpansConverter } from './Converters';
import { SpanRepresentation } from './SpanRepresentation';
import { ListItem } from './ListItem';
import { Colors } from './Color';

@Entity('BaseNote')
export class BaseNote {
  @Id({ isPrimaryKey: true ,autoincrement:true})
  @Columns({ columnName: 'id', types: ColumnType.num })
  id: number
  @Columns({ columnName: 'type', types: ColumnType.num })
  type: Type
  @Columns({ columnName: 'folder', types: ColumnType.num })
  folder: Folder
  @Columns({ columnName: 'color', types: ColumnType.num })
  color: Colors
  @Columns({ columnName: 'title', types: ColumnType.str })
  title: string
  @Columns({ columnName: 'pinned', types: ColumnType.num })
  pinned: boolean
  @Columns({ columnName: 'timestamp', types: ColumnType.num })
  timestamp: number
  @Columns({ columnName: 'labels', types: ColumnType.str })
  @Convert({ converter: LabelConverter, columnType: ColumnType.str })
  labels: List<string>
  @Columns({ columnName: 'body', types: ColumnType.str })
  body: string
  @Columns({ columnName: 'spans', types: ColumnType.str })
  @Convert({ converter: SpansConverter, columnType: ColumnType.str })
  spans: List<SpanRepresentation>
  @Columns({ columnName: 'items', types: ColumnType.str })
  @Convert({ converter: ListItemsConverter, columnType: ColumnType.str })
  items: List<ListItem>

  getId(): number {
    return this.id
  }

  setType(type: Type) {
    this.type = type;
  }

  setFolder(folder: Folder) {
    this.folder = folder;
  }

  setColor(color : Colors) {
    this.color = color;
  }

  setTitle(title: string) {
    this.title = title
  }

  setPinned(pinned: boolean) {
    this.pinned = pinned
  }

  setTimeStamp(timestamp: number) {
    this.timestamp = timestamp
  }

  setLabels(labels: string[]) {
    this.labels = new List()
    for (let obj of labels) {
      this.labels.add(obj)
    }
  }

  setBody(body: string) {
    this.body = body
  }

  setSpans(spans: SpanRepresentation[]) {
    this.spans = new List()
    for (let span of spans) {
      this.spans.add(span)
    }
  }

  setItems(items: ListItem[]) {
    this.items = new List()
    for (let item of items) {
      this.items.add(item)
    }
  }

  constructor(
    type?: Type,
    folder?: Folder,
    color?: Colors,
    title?: string,
    pinned?: boolean,
    timestamp?: number,
    labels?: List<string>,
    body?: string,
    spans?: List<SpanRepresentation>,
    items?:List<ListItem>,
    id?: number
  ) {
    this.id = id;
    this.type = type;
    this.folder = folder;
    this.color = color;
    this.title = title;
    this.pinned = pinned;
    this.timestamp = timestamp;
    this.labels = labels;
    this.body = body;
    this.spans = spans;
    this.items = items;
  }
}